package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyCommentLike;
import cn.wolfcode.trip.base.mapper.StrategyCommentLikeMapper;
import cn.wolfcode.trip.base.service.IStrategyCommentLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class StrategyCommentLikeServiceImpl implements IStrategyCommentLikeService {
    @Autowired
    private StrategyCommentLikeMapper strategyCommentLikeMapper;

    @Override
    public Long checkNumByUserIdAndStrategyCommentId(Long userId, Long strategyCommentId) {
        // 根据userId和strategyCommentId查询数据库是否有记录
        StrategyCommentLike strategyCommentLike =  strategyCommentLikeMapper.queryExistLikeByUserIdAndStrategyCommentId(userId,strategyCommentId);
        if (strategyCommentLike==null){
            // 1保存一条记录
            StrategyCommentLike like = new StrategyCommentLike();
            like.setUserId(userId);
            like.setStrategyCommentId(strategyCommentId);
            like.setTime(new Date());
            strategyCommentLikeMapper.insert(like);
            // 2点赞数记录表保存一条数据
            strategyCommentLikeMapper.insertStrategyCommentNum(userId,strategyCommentId,1L);
            // 3查询当前攻略的点赞数
            return strategyCommentLikeMapper.queryNumByStrategyCommentId(strategyCommentId);
        }else {
            // 删除一条记录
            strategyCommentLikeMapper.delete(userId);
            // 2点赞数记录表删除一条数据
            strategyCommentLikeMapper.deleteStrategyCommentNum(userId,strategyCommentId);
            // 3查询当前攻略的点赞数
            return strategyCommentLikeMapper.queryNumByStrategyCommentId(strategyCommentId);
        }
    }

    @Override
    public Long getNumByStrategyId(Long strategyCommentId) {
        return strategyCommentLikeMapper.queryNumByStrategyCommentId(strategyCommentId);
    }
}
